package com.zdy.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by 逸足天涯
 * on 3/5/2020.
 */

//获取类的信息
public class Test08 {
    public static void main(String[] args) throws ClassNotFoundException,NoSuchFieldException,NoSuchMethodException{
        Class c1 = Class.forName("com.zdy.reflection.User");

        //获取类的名字
        System.out.println(c1.getName()); //报名+ 类名
        System.out.println(c1.getSimpleName()); //类名

        //获取类的属性 //获取不到private属性 可以获得pulic 属性
        System.out.println("****使用getFields()");
        Field[] fields = c1.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        //该方法可以获取私有属性
        System.out.println("****使用getDeclaredFields()");
        Field[] fields1 = c1.getDeclaredFields();
        for (Field field : fields1) {
            System.out.println(field);
        }

        //获得指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);

        // 获取类的方法
        System.out.println("+++++++++++++++++获取本类及其父类的全部公共的方法");
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("==============获取本类的所有方法（包括私有方法）");
        Method[] methods1 = c1.getDeclaredMethods();
        for (Method method : methods1) {
            System.out.println(method);
        }

        //获取指定方法
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);


        //获得指定的构造器
        System.out.println("=====================================");
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }

        Constructor[] constructors1 = c1.getDeclaredConstructors();
        for (Constructor constructor : constructors1) {
            System.out.println("####" + constructor);
        }

        //获得指定的构造器
        Constructor declaredConstructor = c1.getDeclaredConstructor(int.class, String.class, int.class);
        System.out.println("指定的构造器：" + declaredConstructor);


    }
}
